import { recharge, getInsuranceProgramme } from '../../../api/app'
import user from '../../../utils/user'
import {
    guaranteedCount,
    getInsureData
} from '../../api/insurance'
const app = getApp()
Page({
    data: {
        balance: 0,
        price: ['1000','2000','5000','10000'],
        selectIndex: 0,
        productType:'',
        rechargeType: '',
        yearList: ['1年','2年','3年'],
        yearIndex: '0'
    },
    onLoad (options) {
        console.log(options.type, 'options')
        if (options.type !== 'undefined') {
            console.log('长期保进入 或 续费进入')
            this.setData({
                productType: options.type
            })
        } else {
            console.log('日日保进入')
            this.setData({
                productType:  'InsureRechargeGsc'
            })
        }
        console.log(this.data.productType,'productType')
    },
    onShow: function () {
        console.log(user.mchid, '用户的内容是什么',app, 'app的内容是什么')
        if(this.data.productType === 'InsureRechargeGsc'){
            guaranteedCount({
                insureCompanyType:user.insureCompanyType
            }).then(res => {
                this.setData({
                    balance:res.balance
                })
            })
        }else if (this.data.productType === 'payRenew') {
            this.getInsuranceProgramme()
        }else{
            this.getInsureData()
        }
    },
    onSelect(event){
        this.setData({
            selectIndex: event.currentTarget.dataset.index
        })
    },
    onRecharge(){
        console.log('点击啦')
        recharge({
            // InsureRechargeGsc
            useWay: this.data.productType,
            description: '账户充值',
            mchid: user.mchid,
            amount: {
                total: this.data.price[this.data.selectIndex]
            },
            payer: {
                openid: user.openid
            }
        }).then(res=>{
            res.package = res.packageValue
            wx.requestPayment(res).then(()=>{
                wx.navigateTo({
                    url: '/pages/success/success?title=充值成功&content=您可以稍后查看您的账户余额&opinion=0&back=2'
                })
            })
        })
    },
    onOther(){
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge-wechat-other/insurance-recharge-wechat-other?type=' + this.data.productType
        })
    },
    // 长期工投保余额
    async getInsureData () {
        const result = await getInsureData()
        console.log(result,'result')
        this.setData({
            balance: result.accountBalance
        })
    },
    // 切换年份
    onYearIndex (event) {
        const { index } = event.currentTarget.dataset
        this.setData({
            yearIndex:index
        })
    },
    // 根据参数获取内容
    async getInsuranceProgramme () {
        const result = await getInsuranceProgramme({
            dictType:'pay_account_plan'
        })
        console.log(result, 'result')
        this.setData({
            yearList: result
        })
    },
    onPayRecharge () {
        recharge({
            useWay: 'AccountRecharge',
            description: '账户充值',
            amount: {
                total: this.data.yearList[this.data.yearIndex].dictValue
            },
            attach:JSON.stringify({month:this.data.yearList[this.data.yearIndex].dictLabel}),
            payer: {
                openid: user.openid
            }
        }).then(res=>{
            res.package = res.packageValue
            wx.requestPayment(res).then(()=>{
                wx.navigateTo({
                    url: '/pages/success/success?title=续费成功&content=您可以稍后查看您的到期时间&opinion=0&back=2'
                })
            })
        })
    }
})